package com.mirror.controller;

import com.mirror.entity.oldman.Doctor;
import com.mirror.entity.oldman.Result;
import com.mirror.repository.DoctorRepository;
import com.mirror.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Api(description = "医生管理")
@RestController
@RequestMapping("doctor")
public class DoctorController {


    @Autowired
    private DoctorRepository doctorRepository;


    @ApiOperation(value = "医生列表无分页", notes = "医生列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{}", required = true, dataType = "String"),
    })
    @PostMapping("/list")
    public Result list() {
        List<Doctor> blist = doctorRepository.findAllByIsdel(false);
        return ResultUtil.success(blist);
    }

    @ApiOperation(value = "医生列表", notes = "医生列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"page\": \"页码\",\"size\": \"每页数量\"}", required = true, dataType = "String"),
    })
    @PostMapping("/pagelist")
    public Result atList(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        int page = tempData.getInt("page");
        int size = tempData.getInt("size");
        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        Pageable pageable = new PageRequest(page, size, sort);
        Page<Doctor> atPage = doctorRepository.findAllByIsdel(pageable, false);
        return ResultUtil.success(atPage);
    }


    @ApiOperation(value = "添加房间", notes = "添加房间")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"floorId\": \"楼层id\",\"roomName\": \"房间名\",\"memo\": \"备注\",\"roomNum\": \"房间号\"}", required = true, dataType = "String"),
    })
    @PostMapping("/save")
    public Result save(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        String ethnic = tempData.getString("ethnic");
        String idCard = tempData.getString("idCard");
        String name = tempData.getString("name");
        String phone = tempData.getString("phone");
        String dtype = tempData.getString("dtype");
        String sex = tempData.getString("sex");
        String url = tempData.getString("url");
        Doctor d = new Doctor();
        d.setCreateDate(new Date());
        d.setEthnic(ethnic);
        d.setIdCard(idCard);
        d.setName(name);
        d.setPhone(phone);
        d.setDtype(dtype);
        d.setSex(sex);
        d.setUrl(url);
        d.setIsdel(false);
        return ResultUtil.success(doctorRepository.save(d));
    }

    @ApiOperation(value = "修改房间", notes = "修改房间")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"roomId\": \"房间id\",\"floorId\": \"楼层id\",\"roomName\": \"房间名\",\"memo\": \"备注\",\"roomNum\": \"房间号\"}", required = true, dataType = "String"),
    })
    @PostMapping("/edit")
    public Result edit(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        String ethnic = tempData.getString("ethnic");
        String idCard = tempData.getString("idCard");
        String name = tempData.getString("name");
        String phone = tempData.getString("phone");
        String dtype = tempData.getString("dtype");
        String sex = tempData.getString("sex");
        String url = tempData.getString("url");
        String doctorId = tempData.getString("doctorId");
        Doctor d = doctorRepository.findOne(doctorId);
        if (d == null) {
            return ResultUtil.error(-1, "获取医生失败！");
        }
        d.setEthnic(ethnic);
        d.setIdCard(idCard);
        d.setName(name);
        d.setDtype(dtype);
        d.setPhone(phone);
        d.setSex(sex);
        d.setUrl(url);
        return ResultUtil.success(doctorRepository.save(d));
    }

    @ApiOperation(value = "删除房间", notes = "删除房间")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"roomId\": \"房间id\"}", required = true, dataType = "String"),
    })
    @PostMapping("/del")
    public Result del(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        String doctorId = tempData.getString("doctorId");
        Doctor d = doctorRepository.findOne(doctorId);
        if (d == null) {
            return ResultUtil.error(-1, "获取医生失败！");
        }
        d.setIsdel(true);
        return ResultUtil.success(doctorRepository.save(d));
    }

}

